package jalus.dao;

import jalus.model.AccessLog;
import jalus.model.PMF;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class AccessLogDAO {

	@SuppressWarnings("unchecked")
	public List<AccessLog> getAllAccessLog() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(AccessLog.class);
	    List<AccessLog> accessLogList;
	    try{
	    	accessLogList = (List<AccessLog>) query.execute();
	    }finally{
	    	query.closeAll();
	    }
	    return accessLogList;
	}

	public void addAccessLog(AccessLog accessLog){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(accessLog);
		}finally{
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<AccessLog> getAccesslogByDate(Date beginDate, Date endDate){
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(AccessLog.class);
	    query.setOrdering("date desc");
	    query.setFilter("date > beginDate && date < endDate");
	    query.declareParameters("java.util.Date beginDate, java.util.Date endDate");
	    List<AccessLog> accessLogList;
	    try{
	    	accessLogList = (List<AccessLog>) query.execute(beginDate, endDate);
	    }finally{
	    	query.closeAll();
	    }
	    return accessLogList;
	}
	
}
